package com.dwxt.cdms.api.service.impl;


import com.dwxt.cdms.api.common.utils.StringUtil;
import com.dwxt.cdms.api.service.InformationService;
import com.dwxt.cdms.dao.info.InformationMapper;
import com.dwxt.cdms.entity.info.InformationExample;
import com.dwxt.cdms.entity.info.InformationWithBLOBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class InfomationServiceImpl implements InformationService {

    @Autowired
    private InformationMapper informationMapper;

    @Override
    public int insert(InformationWithBLOBs information) {
        informationMapper.updateByPriority(information.getPriority());
        return informationMapper.insertSelective(information);
    }

    @Override
    public int update(InformationWithBLOBs information) {
        InformationWithBLOBs informationWithBLOBs = informationMapper.selectByPrimaryKey(information.getId());
        if (informationWithBLOBs.getPriority()!=information.getPriority()){
            informationMapper.updateByPriority(information.getPriority());
        }
        return informationMapper.updateByPrimaryKeySelective(information);
    }

    @Override
    public int delete(String id) {
        return informationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public InformationWithBLOBs selectById(String id) {
        return informationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<InformationWithBLOBs> selectByObj(InformationWithBLOBs information) {
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        if (information.getTitle()!=null){
            criteria.andTitleEqualTo(information.getTitle());
        }
        if (information.getAuther()!=null){
            criteria.andAutherEqualTo(information.getAuther());
        }
        if (StringUtil.isNotBlank(information.getType())){
            criteria.andAdTypeIdEqualTo(information.getType());
        }else {
            List<String> typeIds = new ArrayList<>();
            typeIds.add("13");
            typeIds.add("14");
            criteria.andAdTypeIdNotIn(typeIds);
        }
        if (information.getIsRecommend()!=null){
            criteria.andIsRecommendEqualTo(information.getIsRecommend());
        }
       /* if ("homeAd".equals(information.getType())) {//广告
            criteria.andAdTypeIdEqualTo("13");
        } else if ("findAd".equals(information.getType())) {
            criteria.andAdTypeIdEqualTo("14");
        } else {
            List<String> typeIds = new ArrayList<>();
            typeIds.add("13");
            typeIds.add("14");
            criteria.andAdTypeIdNotIn(typeIds);
        }*/
        informationExample.setOrderByClause("create_time desc");
        return informationMapper.selectByExampleWithBLOBs(informationExample);
    }
}
